home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1F0JW7M (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  13.1 KB  |  371 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.AbstractAction;
  4. import com.sun.java.swing.BorderFactory;
  5. import com.sun.java.swing.BoxLayout;
  6. import com.sun.java.swing.JComboBox;
  7. import com.sun.java.swing.JComponent;
  8. import com.sun.java.swing.JDialog;
  9. import com.sun.java.swing.JList;
  10. import com.sun.java.swing.JPopupMenu;
  11. import com.sun.java.swing.JRootPane;
  12. import com.sun.java.swing.JScrollPane;
  13. import com.sun.java.swing.SwingUtilities;
  14. import com.sun.java.swing.Timer;
  15. import com.sun.java.swing.UIManager;
  16. import com.sun.java.swing.border.Border;
  17. import com.sun.java.swing.event.ListSelectionListener;
  18. import java.awt.AWTEvent;
  19. import java.awt.Color;
  20. import java.awt.Component;
  21. import java.awt.Container;
  22. import java.awt.Dialog;
  23. import java.awt.Dimension;
  24. import java.awt.Point;
  25. import java.awt.Rectangle;
  26. import java.awt.Toolkit;
  27. import java.awt.Window;
  28. import java.awt.event.InputEvent;
  29. import java.awt.event.ItemListener;
  30. import java.awt.event.KeyListener;
  31. import java.awt.event.MouseEvent;
  32. import java.awt.event.MouseListener;
  33. import java.awt.event.MouseMotionListener;
  34. import java.beans.PropertyChangeListener;
  35. import java.util.EventObject;
  36.  
  37. public class BasicComboPopup extends JPopupMenu implements ComboPopup {
  38.    protected JComboBox comboBox;
  39.    protected JList list;
  40.    protected JScrollPane scroller;
  41.    protected boolean valueIsAdjusting = false;
  42.    protected MouseMotionListener mouseMotionListener;
  43.    protected MouseListener mouseListener;
  44.    protected KeyListener keyListener;
  45.    protected ListSelectionListener listSelectionListener;
  46.    protected MouseListener listMouseListener;
  47.    protected MouseMotionListener listMouseMotionListener;
  48.    protected PropertyChangeListener propertyChangeListener;
  49.    protected ItemListener itemListener;
  50.    protected Timer autoscrollTimer;
  51.    protected boolean hasEntered = false;
  52.    protected boolean isAutoScrolling = false;
  53.    protected int scrollDirection = 0;
  54.    protected static final int SCROLL_UP = 0;
  55.    protected static final int SCROLL_DOWN = 1;
  56.  
  57.    public BasicComboPopup(JComboBox combo) {
  58.       this.comboBox = combo;
  59.       this.mouseListener = this.createMouseListener();
  60.       this.mouseMotionListener = this.createMouseMotionListener();
  61.       this.keyListener = this.createKeyListener();
  62.       this.listSelectionListener = this.createListSelectionListener();
  63.       this.listMouseListener = this.createListMouseListener();
  64.       this.listMouseMotionListener = this.createListMouseMotionListener();
  65.       this.propertyChangeListener = this.createPropertyChangeListener();
  66.       this.itemListener = this.createItemListener();
  67.       this.list = this.createList();
  68.       this.configureList();
  69.       this.scroller = this.createScroller();
  70.       this.configureScroller();
  71.       this.configurePopup();
  72.       this.addComboBoxListeners();
  73.    }
  74.  
  75.    protected void addComboBoxListeners() {
  76.       this.comboBox.addPropertyChangeListener(this.propertyChangeListener);
  77.       this.comboBox.addItemListener(this.itemListener);
  78.    }
  79.  
  80.    protected void addListListeners() {
  81.       this.list.addListSelectionListener(this.listSelectionListener);
  82.       this.list.addMouseMotionListener(this.listMouseMotionListener);
  83.       this.list.addMouseListener(this.listMouseListener);
  84.    }
  85.  
  86.    protected void autoScrollDown() {
  87.       int index = this.list.getSelectedIndex();
  88.       int lastItem = this.list.getModel().getSize() - 1;
  89.       if (index < lastItem) {
  90.          this.valueIsAdjusting = true;
  91.          this.list.setSelectedIndex(index + 1);
  92.          this.valueIsAdjusting = false;
  93.          this.list.ensureIndexIsVisible(index + 1);
  94.       }
  95.  
  96.    }
  97.  
  98.    protected void autoScrollUp() {
  99.       int index = this.list.getSelectedIndex();
  100.       if (index > 0) {
  101.          this.valueIsAdjusting = true;
  102.          this.list.setSelectedIndex(index - 1);
  103.          this.valueIsAdjusting = false;
  104.          this.list.ensureIndexIsVisible(index - 1);
  105.       }
  106.  
  107.    }
  108.  
  109.    protected Rectangle computePopupBounds(int px, int py, int pw, int ph) {
  110.       Rectangle r = new Rectangle(px, py, pw, ph);
  111.       boolean inModalDialog = this.inModalDialog();
  112.       Rectangle absBounds;
  113.       if (inModalDialog) {
  114.          Dialog dlg = this.getDialog();
  115.          if (dlg instanceof JDialog) {
  116.             JRootPane rp = ((JDialog)dlg).getRootPane();
  117.             Point p = ((Component)rp).getLocationOnScreen();
  118.             absBounds = ((Component)rp).getBounds();
  119.             absBounds.x = p.x;
  120.             absBounds.y = p.y;
  121.          } else {
  122.             absBounds = ((Component)dlg).getBounds();
  123.          }
  124.  
  125.          Point scrSize = new Point(absBounds.x, absBounds.y);
  126.          SwingUtilities.convertPointFromScreen(scrSize, this.comboBox);
  127.          absBounds.x = scrSize.x;
  128.          absBounds.y = scrSize.y;
  129.       } else {
  130.          Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
  131.          absBounds = new Rectangle();
  132.          Point p = new Point(0, 0);
  133.          SwingUtilities.convertPointFromScreen(p, this.comboBox);
  134.          absBounds.x = p.x;
  135.          absBounds.y = p.y;
  136.          absBounds.width = scrSize.width;
  137.          absBounds.height = scrSize.height;
  138.       }
  139.  
  140.       if (SwingUtilities.isRectangleContainingRectangle(absBounds, r)) {
  141.          return r;
  142.       } else {
  143.          Rectangle r2 = new Rectangle(0, -r.height, r.width, r.height);
  144.          if (SwingUtilities.isRectangleContainingRectangle(absBounds, r2)) {
  145.             return r2;
  146.          } else if (inModalDialog) {
  147.             SwingUtilities.computeIntersection(absBounds.x, absBounds.y, absBounds.width, absBounds.height, r);
  148.             SwingUtilities.computeIntersection(absBounds.x, absBounds.y, absBounds.width, absBounds.height, r2);
  149.             return r.height > r2.height ? r : r2;
  150.          } else {
  151.             return r2;
  152.          }
  153.       }
  154.    }
  155.  
  156.    protected void configureList() {
  157.       this.list.setFont(this.comboBox.getFont());
  158.       this.list.setForeground(this.comboBox.getForeground());
  159.       this.list.setBackground(this.comboBox.getBackground());
  160.       this.list.setSelectionForeground(UIManager.getColor("ComboBox.selectedForeground"));
  161.       this.list.setSelectionBackground(UIManager.getColor("ComboBox.selectedBackground"));
  162.       this.list.setBorder((Border)null);
  163.       this.list.setCellRenderer(this.comboBox.getRenderer());
  164.       this.list.setRequestFocusEnabled(false);
  165.       this.addListListeners();
  166.    }
  167.  
  168.    protected void configurePopup() {
  169.       ((Container)this).setLayout(new BoxLayout(this, 1));
  170.       ((JPopupMenu)this).setBorderPainted(true);
  171.       ((JComponent)this).setBorder(BorderFactory.createLineBorder(Color.black));
  172.       ((JComponent)this).setOpaque(false);
  173.       ((JPopupMenu)this).add(this.scroller);
  174.       ((JComponent)this).setDoubleBuffered(true);
  175.       ((JComponent)this).setRequestFocusEnabled(false);
  176.    }
  177.  
  178.    protected void configureScroller() {
  179.       this.scroller.setRequestFocusEnabled(false);
  180.       this.scroller.getVerticalScrollBar().setRequestFocusEnabled(false);
  181.       this.scroller.setBorder((Border)null);
  182.    }
  183.  
  184.    protected MouseEvent convertMouseEvent(MouseEvent e) {
  185.       Point convertedPoint = SwingUtilities.convertPoint((Component)((EventObject)e).getSource(), e.getPoint(), this.list);
  186.       MouseEvent newEvent = new MouseEvent((Component)((EventObject)e).getSource(), ((AWTEvent)e).getID(), ((InputEvent)e).getWhen(), ((InputEvent)e).getModifiers(), convertedPoint.x, convertedPoint.y, ((InputEvent)e).getModifiers(), e.isPopupTrigger());
  187.       return newEvent;
  188.    }
  189.  
  190.    protected ItemListener createItemListener() {
  191.       return new ComboItemListener(this);
  192.    }
  193.  
  194.    protected KeyListener createKeyListener() {
  195.       return new InvocationKeyListener(this);
  196.    }
  197.  
  198.    protected JList createList() {
  199.       return new JList(this.comboBox.getModel());
  200.    }
  201.  
  202.    protected MouseListener createListMouseListener() {
  203.       return new ListMouseListener(this);
  204.    }
  205.  
  206.    protected MouseMotionListener createListMouseMotionListener() {
  207.       return new ListMouseMotionListener(this);
  208.    }
  209.  
  210.    protected ListSelectionListener createListSelectionListener() {
  211.       return new ListSelListener(this);
  212.    }
  213.  
  214.    protected MouseListener createMouseListener() {
  215.       return new InvocationMouseListener(this);
  216.    }
  217.  
  218.    protected MouseMotionListener createMouseMotionListener() {
  219.       return new InvocationMouseMotionListener(this);
  220.    }
  221.  
  222.    protected PropertyChangeListener createPropertyChangeListener() {
  223.       return new ComboPropertyChangeListener(this);
  224.    }
  225.  
  226.    protected JScrollPane createScroller() {
  227.       return new JScrollPane(this.list, 20, 31);
  228.    }
  229.  
  230.    protected void delegateFocus(MouseEvent e) {
  231.       if (this.comboBox.isEditable()) {
  232.          this.comboBox.getEditor().getEditorComponent().requestFocus();
  233.       } else {
  234.          this.comboBox.requestFocus();
  235.       }
  236.  
  237.    }
  238.  
  239.    private Dialog getDialog() {
  240.       Container parent;
  241.       for(parent = this.comboBox.getParent(); parent != null && !(parent instanceof Dialog) && !(parent instanceof Window); parent = ((Component)parent).getParent()) {
  242.       }
  243.  
  244.       return parent instanceof Dialog ? (Dialog)parent : null;
  245.    }
  246.  
  247.    public KeyListener getKeyListener() {
  248.       return this.keyListener;
  249.    }
  250.  
  251.    public MouseListener getMouseListener() {
  252.       return this.mouseListener;
  253.    }
  254.  
  255.    public MouseMotionListener getMouseMotionListener() {
  256.       return this.mouseMotionListener;
  257.    }
  258.  
  259.    protected int getPopupHeightForRowCount(int maxRowCount) {
  260.       int currentElementCount = this.comboBox.getModel().getSize();
  261.       if (currentElementCount > 0) {
  262.          Rectangle r = this.list.getCellBounds(0, 0);
  263.          return maxRowCount < currentElementCount ? r.height * maxRowCount + 2 : r.height * currentElementCount + 2;
  264.       } else {
  265.          return 100;
  266.       }
  267.    }
  268.  
  269.    public void hide() {
  270.       ((JPopupMenu)this).setVisible(false);
  271.       this.comboBox.repaint();
  272.    }
  273.  
  274.    private boolean inModalDialog() {
  275.       return this.getDialog() != null;
  276.    }
  277.  
  278.    public boolean isFocusTraversable() {
  279.       return false;
  280.    }
  281.  
  282.    public void show() {
  283.       Dimension popupSize = this.comboBox.getSize();
  284.       popupSize.setSize(popupSize.width, this.getPopupHeightForRowCount(this.comboBox.getMaximumRowCount()));
  285.       this.scroller.setMaximumSize(popupSize);
  286.       this.scroller.setPreferredSize(popupSize);
  287.       this.scroller.setMinimumSize(popupSize);
  288.       Rectangle popupBounds = this.computePopupBounds(0, this.comboBox.getBounds().height, popupSize.width, popupSize.height);
  289.       this.list.invalidate();
  290.       this.list.setSelectedIndex(this.comboBox.getSelectedIndex());
  291.       this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
  292.       ((JPopupMenu)this).setLightWeightPopupEnabled(this.comboBox.isLightWeightPopupEnabled());
  293.       ((JPopupMenu)this).show(this.comboBox, popupBounds.x, popupBounds.y);
  294.    }
  295.  
  296.    protected void startAutoScrolling(int direction) {
  297.       if (this.isAutoScrolling) {
  298.          this.autoscrollTimer.stop();
  299.       }
  300.  
  301.       this.isAutoScrolling = true;
  302.       if (direction == 0) {
  303.          this.scrollDirection = 0;
  304.          Point convertedPoint = SwingUtilities.convertPoint(this.scroller, new Point(1, 1), this.list);
  305.          int top = this.list.locationToIndex(convertedPoint);
  306.          this.valueIsAdjusting = true;
  307.          this.list.setSelectedIndex(top);
  308.          this.valueIsAdjusting = false;
  309.          AbstractAction timerAction = new 1(this);
  310.          this.autoscrollTimer = new Timer(100, timerAction);
  311.       } else if (direction == 1) {
  312.          this.scrollDirection = 1;
  313.          Dimension size = this.scroller.getSize();
  314.          Point convertedPoint = SwingUtilities.convertPoint(this.scroller, new Point(1, size.height - 1 - 2), this.list);
  315.          int bottom = this.list.locationToIndex(convertedPoint);
  316.          this.valueIsAdjusting = true;
  317.          this.list.setSelectedIndex(bottom);
  318.          this.valueIsAdjusting = false;
  319.          AbstractAction timerAction = new 2(this);
  320.          this.autoscrollTimer = new Timer(100, timerAction);
  321.       }
  322.  
  323.       this.autoscrollTimer.start();
  324.    }
  325.  
  326.    protected void stopAutoScrolling() {
  327.       this.isAutoScrolling = false;
  328.       if (this.autoscrollTimer != null) {
  329.          this.autoscrollTimer.stop();
  330.          this.autoscrollTimer = null;
  331.       }
  332.  
  333.    }
  334.  
  335.    protected void togglePopup() {
  336.       if (((JPopupMenu)this).isVisible()) {
  337.          this.hide();
  338.       } else {
  339.          this.show();
  340.       }
  341.  
  342.    }
  343.  
  344.    public void uninstallingUI() {
  345.       this.comboBox.removePropertyChangeListener(this.propertyChangeListener);
  346.       this.comboBox.removeItemListener(this.itemListener);
  347.    }
  348.  
  349.    protected void updateListBoxSelectionForEvent(MouseEvent anEvent, boolean shouldScroll) {
  350.       Point location = anEvent.getPoint();
  351.       if (this.list != null) {
  352.          int index = this.list.locationToIndex(location);
  353.          if (index == -1) {
  354.             if (location.y < 0) {
  355.                index = 0;
  356.             } else {
  357.                index = this.comboBox.getModel().getSize() - 1;
  358.             }
  359.          }
  360.  
  361.          if (this.list.getSelectedIndex() != index) {
  362.             this.list.setSelectedIndex(index);
  363.             if (shouldScroll) {
  364.                this.list.ensureIndexIsVisible(index);
  365.             }
  366.          }
  367.  
  368.       }
  369.    }
  370. }
  371.